package com.yjs.app.trade.transaction.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yjs.app.trade.transaction.util.PropertiesUtils;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

@Service
public class JPushAllService {
	private static final String APP_KEY = PropertiesUtils.getProperty("jpush_app_key");
	private static final String SECRET = PropertiesUtils.getProperty("jpush_secret");
	private static final boolean isIosPro = Boolean.parseBoolean(PropertiesUtils.getProperty("jpush_ios_pro").toString());
	
	private Logger log = LoggerFactory.getLogger(Logger.class);
	
	public void jpushAll(Map<String, String> param,Map<String, String> parameter, List<String> list) { 
		
		//创建JPushClient
		JPushClient jpushClient = new JPushClient(SECRET, APP_KEY);
		//创建option
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.all())  //所有平台的用户
				.setAudience(Audience.registrationId(list))//registrationId指定用户
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder() //发送ios
								.setAlert(param.get("msg")) //消息体
								.setBadge(+1)
								.setSound("happy") //ios提示音
								.addExtras(parameter) //附加参数
								.build())
						.addPlatformNotification(AndroidNotification.newBuilder() //发送android
								.addExtras(parameter) //附加参数
								.setAlert(param.get("msg")) //消息体
								.build())
						.build())
				.setOptions(Options.newBuilder().setApnsProduction(isIosPro).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
				.setMessage(Message.newBuilder().setMsgContent(param.get("msg")).addExtras(param).build())//自定义信息
				.build();
		PushResult pu = new PushResult();
		try {
			pu = jpushClient.sendPush(payload);
//			log.info(pu.toString());
		} catch (APIConnectionException e) {
			log.error(pu.toString());
			log.error("Jpush推送消息出错~~~~~" + e);
		} catch (APIRequestException e) {
			log.error(pu.toString());
			log.error("Jpush推送消息出错~~~~~" + e);
		}    
	}
	
	public static void main(String[] args) {
		 List<String> list=new ArrayList<String>();
		//设置推送参数
		//这里同学们就可以自定义推送参数了
		Map<String, String> parm = new HashMap<String, String>();
		Map<String, String> parms = new HashMap<String, String>();
		//设备id,指定设备推送id
		//parm.put("id", "1114a89792f516faf0a");
		//设置提示信息,内容是文章标题
		parm.put("msg","测试测试,收到请联系发送人");
		JPushAllService jPushAllService = new JPushAllService();
		jPushAllService.jpushAll(parm,parms,list);
 
	}


}
